package com.virgo.demo.support.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.common.entity.ListDataVo;
import com.virgo.common.exception.IApplicationException;
import com.virgo.common.utils.ICommonUtils;
import com.virgo.demo.support.converter.DictTypeConverter;
import com.virgo.demo.support.domain.DictDataDo;
import com.virgo.demo.support.domain.DictTypeDo;
import com.virgo.demo.support.domain.bo.DictTypeBo;
import com.virgo.demo.support.domain.vo.DictTypeVo;
import com.virgo.demo.support.mapper.DictDataMapper;
import com.virgo.demo.support.mapper.DictTypeMapper;
import com.virgo.demo.support.service.DictTypeService;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictTypeDo> implements DictTypeService {

	private final DictTypeMapper dictTypeMapper;

	private final DictTypeConverter dictTypeConverter;

	private final DictDataMapper dictDataMapper;

	/**
	 * 根据字典标识查找
	 * @param dictType String
	 * @return DictTypeDo
	 */
	@Override
	public DictTypeDo loadByType(String dictType) {
		LambdaQueryWrapper<DictTypeDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(DictTypeDo::getDictType,dictType);
		queryWrapper.last(" limit 1");
		return getOne(queryWrapper);
	}

	/**
	 * 编辑字典
	 * @param dictTypeBo DictTypeBo
	 */
	@Override
	public void saveDictTyp(DictTypeBo dictTypeBo) {
		DictTypeDo exist = loadByType(dictTypeBo.getDictType());
		DictTypeDo dictTypeDo = new DictTypeDo();
		BeanUtils.copyProperties(dictTypeBo,dictTypeDo);
		if (!ObjectUtils.isEmpty(exist) && !exist.getDictId().equals(dictTypeDo.getDictId())) {
			throw new IApplicationException("字典-" + dictTypeDo.getDictType() + "已存在");
		}
		if (ObjectUtils.isEmpty(dictTypeDo.getDictId()) || dictTypeDo.getDictId().compareTo(0L) < 1) {
			save(dictTypeDo);
			return;
		}
		updateById(dictTypeDo);
	}

	/**
	 * 删除字典
	 * @param ids String
	 */
	@Override
	public void deleteDictType(String ids) {
		List<Long> idList = Arrays.asList(ICommonUtils.stringToArray(ids, ","));
		LambdaQueryWrapper<DictTypeDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(DictTypeDo::getDictId,idList);
		queryWrapper.select(DictTypeDo::getDictType);
		List<DictTypeDo> dictTypeDos = dictTypeMapper.selectList(queryWrapper);
		if (ObjectUtils.isEmpty(dictTypeDos)) {
			return;
		}
		List<String> dictTypeList = dictTypeDos.stream().map(DictTypeDo::getDictType).distinct().toList();
		LambdaQueryWrapper<DictDataDo> dictTypeDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
		dictTypeDoLambdaQueryWrapper.in(DictDataDo::getDictType,dictTypeList);
		long dictDataNum = dictDataMapper.selectCount(dictTypeDoLambdaQueryWrapper);
		if (dictDataNum > 0L) {
			throw new IApplicationException("存在字典值，不能删除");
		}
		removeByIds(idList);
	}

	/**
	 * 标签列表
	 * @param request HttpServletRequest
	 * @return ListDataVo<DictTypeVo>
	 */
	@Override
	public ListDataVo<DictTypeVo> findList(HttpServletRequest request) {
		long currentPage = IRequestUtils.getLong(request,"page");
		long pageSize = IRequestUtils.getLong(request,"pageSize");
		currentPage = Math.max(currentPage,1);
		pageSize = Math.max(pageSize,10);

		LambdaQueryWrapper<DictTypeDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(DictTypeDo::getDictId);

		String dictName = IRequestUtils.getString(request,"dictName","");
		if (!ObjectUtils.isEmpty(dictName)) {
			queryWrapper.like(DictTypeDo::getDictName,dictName);
		}

		Page<DictTypeDo> page = new Page<>(currentPage, pageSize);
		IPage<DictTypeDo> iPage = dictTypeMapper.selectPage(page, queryWrapper);
		return new ListDataVo<>(iPage.getTotal(),iPage.getPages(),dictTypeConverter.convert(iPage.getRecords(), DictTypeVo.class));
	}
}